// pages/mine/history.js
const app = getApp();
Page({
  data: {
    userName: "引号",
    userAvatar: "https://studynest.top/img/userImage.jpg"
  },
  updataInfo() {
    this.setData({
      userName: app.globalData.userInfo.userName,
      userAvatar: app.globalData.userInfo.userAvatar
    })
    this.checkAvatar();
  },
  checkAvatar() {
    //头像为空，使用默认头像
    if (this.data.userAvatar == null || this.data.userAvatar == "" || this.data.userAvatar == '') {
      console.log(this.data.userAvatar)
      this.setData({
        userAvatar: "https://studynest.top/img/userImage.jpg"
      })
    }
  },
  async onLoad(options) {
    this.setData({
      userName: app.globalData.userInfo.userName,
      userAvatar: app.globalData.userInfo.userAvatar
    })
    await this.checkAvatar();
  },
  navToTest(){
    wx.navigateTo({
      url: '/pages/test/test',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    //更新选中的tarbar下标并且更新，不可删否则bug
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selectIndex: 2
      })
      console.warn('mine')
    }
    //获取最新用户信息
    const userData = await getApp().login()
    this.setData({
      userName: userData.userName,
      userAvatar: userData.userAvatar
    })
    await this.checkAvatar();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.updataInfo()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})